/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Login;
import org.ipti.br.tag.entity.Login_;
import org.ipti.br.tag.entity.Person_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author israel
 */
@Stateless
public class LoginFacade extends AbstractFacade<Login> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LoginFacade() {
        super(Login.class);
    }

    @Override
    public void create(Login login) {
        getEntityManager().persist(login);
    }

    @Override
    public void edit(Login login) {
        getEntityManager().merge(login);
    }

    public Login findLoginPerUserName(String userName) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Login> cq = cb.createQuery(Login.class);
        Root<Login> root = cq.from(Login.class);
        cq.where(cb.equal(root.get(Login_.userName), userName));
        Login login = null;
        try {
            login = getEntityManager().createQuery(cq).getSingleResult();
        } catch (Exception e) {
            return null;
        }
        return login;
    }
    
    public List<Login> searchLogins(String userName) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Login> cq = cb.createQuery(Login.class);
        Root<Login> root = cq.from(Login.class);
        cq.where(cb.equal(root.get(Login_.userName), userName));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public Login findLoginPerPerson(Long personID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Login> cq = cb.createQuery(Login.class);
        Root<Login> root = cq.from(Login.class);
        cq.where(cb.equal(root.get(Login_.person).get(Person_.id), personID));
        Login login = null;
        try {
            login = getEntityManager().createQuery(cq).getSingleResult();
        } catch (Exception e) {
            return null;
        }
        return login;
    }

    public List<Login> modifiedLoginBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Login> cq = cb.createQuery(Login.class);
        Root<Login> from = cq.from(Login.class);
        Predicate predicate1 = cb.notEqual(from.get(Login_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Login_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Login_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<Login> findAllOrder() {
        return findAllOrderBy(Login_.userName);
    }
}
